<template>
  <view class="cart-box">
    <view class="havGoods-box" v-if="cart.length != 0">
      <!-- 收货地址区域 -->
      <my-address></my-address>
      
      <!-- 购物车商品列表的标题区域 -->
      <view class="cart-title">
        <!-- 左侧图标 -->
        <uni-icons type="shop" size="18"></uni-icons>
        <!-- 右侧文字 -->
        <text class="cart-title-text">购物车</text>
      </view>
      
      <!-- 购物车商品项区域 -->
      <view class="cart-goods-box">
        <!-- uni-swipe-action 和 uni-swipe-action-item 搭配使用，达到滑动出现一个 uni-swipe-action-item -->
        <uni-swipe-action>
          <block class="cart-goods" v-for="(goods, index) in cart" :key="index">
            <uni-swipe-action-item :right-options="options" @click="deleteOne(goods)">
              <my-goods :goods="goods" :showRadio="true" :showNumberBox="true" @changeState="changeStateHandler" @changeNum="changeNumHandler"></my-goods>
            </uni-swipe-action-item>
          </block>
        </uni-swipe-action>
      </view>
      
      <!-- 结算区域 -->
      <view class="settle-container">
        <my-settle></my-settle>
      </view>
    </view>
    <view class="empty-cart" v-else>
      <image src="/static/empty_cart.jpeg" class="empty-img"></image>
      <text class="tip-text">空空如也~</text>
    </view>
  </view>
</template>

<script>
  import badgeMixins from '@/mixins/tabbar-badge.js'
  import { mapState, mapMutations, mapGetters } from 'vuex'
  
  export default {
    mixins: [badgeMixins],
    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      total() {
        // total(购物车中商品总数)每次变化的时候，就调用一下mixins里面的setBadge 来设置最新的total值
        this.setBadge()
      }
    },
    data() {
      return {
        se: 3,
        options: [
          {
            text: '删除',
            style: {
              backgroundColor: '#C00000'
            }
          }
        ]
      };
    },
    methods: {
      ...mapMutations('m_cart', ['changeGoodsState', 'changeGoodsCount', 'deleteGoods']),
      changeStateHandler(e) {
        // console.log(e) // {goods_id: 1,  goods_state: false}
        // cart 通过 find 依照 goods_id 找到对应的 goods ，进行修改 goods_state
        this.changeGoodsState(e)
      },
      changeNumHandler(e) {
        this.changeGoodsCount(e) // 修改该商品count
      },
      deleteOne(goods) {
        // 询问是否确认删除
        uni.showModal({
          title: '确认删除吗',
          content: '从购物车中移除该商品',
          success: (res) => {
            if(res.confirm) {
              this.deleteGoods(goods.goods_id) // 传入要删除的商品id
            }
          }
        })
      }
    },
    onLoad() {
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #FFFFFF;
  }
  .cart-box {
    padding-bottom: 50px;
  }
  .cart-title {
    display: flex;
    align-items: center;
    height: 40px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;
    background-color: #FFFFFF;
    .cart-title-text {
      margin-left: 10px;
      font-size: 14px;
    }
  }
  
  .cart-goods-box {
    padding: 0 5px;
    background-color: #FFFFFF;
  }
  
  .settle-container {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
  
  .empty-cart {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-top: 150px;
    background-color: #FFFFFF;
    
    .empty-img {
      width: 90px;
      height: 90px;
    }
    
    .tip-text {
      font-size: 12px;
      color: gray;
      margin-top: 10px;
      padding-left: 10px;
    }
  }
</style>
